$(function() {
	//用户管理
	//----角色管理
	/**
	 * Init
	 *
	 * 
	 * |-- 
	 * |   `-- 
	 * |   `-- 
	 * |	   `-- 
	 * |		   `-- 
	 * |		   `-- 
	 * |			   `-- 
	 * |		   `-- 
	 * |-- 
	 * |-- 
	 */
	var ad = appData();

	var tmplObj = null;
	var newData=[];
	var arrys = []
	console.log('已加载')

	DomNameStart()

	function DomNameStart() {

		
		//侧面添加class、索引加载
		//$('.sys-sidebar ul li').eq(0).find('ul li ').eq(0).addClass('active')

		var name = $('.sys-sidebar').find('li .active a').text()

		$("#TopName").text(name) //替换头部日志名称
		
		
		  
		
		
		showTable(true,1)
		
		
		$("#add").on('click',function(){
			$("#addRole").show()
		})
		$("#close").on('click',function(){
			$("#addRole").hide()
		})
		$("#cancel").on('click',function(){
			$("#addRole").hide()
			$("#roleVal").val('')
			loadZtree()
			
			
			
		})
		
		$("#save").on('click',function(){
				if($("#roleVal").val()==''){
					
					_as.jAlert('请填写角色名称')
					
				}else if(newData.length==0){
					
					_as.jAlert('请选择角色权限')
					
				}else{
					$("#addRole").hide()
				
					var param = {
						"value":$("#roleVal").val(),
						"ztreeId":newData
					}
					ad.insert("ROLE_NAME_SEND",param,function(data){
						_as.jAlert('添加成功')
					})
				}
				
		})
	}
	
	
	//表格show加载
	function showTable(dontRenderPager, page) {
  		
		var params = {
			"page":page,
			"keyValue":$("#searchVal").val() ,
			"pageSize": appApi('PAGE_SIZE')
		}
  		
		ad.insert("ROLE_MAN_LIST", params, function(data) {
			
			$("#tableBody").empty();
			if (data.length === 0) {
				$("#tableBody").append("<tr><td colspan='7' class='colspanCss'><h1>暂无数据</h1></td></tr>");
				return false;
			}
		
			
			
			$.get('scripts/app/safe/userManage/roleMan.tmpl', function(tmpl) {
			
				
				$("#tableBody").empty().append($.tmpl(tmpl, data))
				
				if (dontRenderPager) {
					renderPager(data.totalRecord);
				}
			})
			
		})
	}
	
	function renderPager(totalRecord, nowPage) {
		$("div.page").pagination(totalRecord, appApi("PAGE_SIZE"), {
			callback: function(page_id, jq) {
				pageNumNow1 = page_id + 1;
				showTable(false, pageNumNow1);
			}
		}, nowPage);
	};
	
	domCase()
	function domCase() {
		$("#search1").on('click',function(){
			
			showTable(true,1)
		})
	}
	loadZtree()
	function loadZtree() {
		var zNodes = [];
		ad.selectList("ROLE_ZTREE_NAME",{},function(data){
			for(var i=0;i<data.length;i++){
						zNodes.push({
									id: data[i].id,
									pId: data[i].pId,
									name: data[i].name,
									open: data[i].open
								})
			}
			
			 var setting = {
			    check: {
			      enable: true
			    },
			    data: {
			      simpleData: {
			        enable: true
			      }
			    },
			    callback: {
					onCheck: zTreeOnCheck
				}
			  };
			
			
			
			function zTreeOnCheck(event, treeId, treeNode) {
				var ztreeId = []
    			ztreeId.push(treeNode.id);
    			if(treeNode.checked){
    				for(var i=0;i<ztreeId.length;i++){
    					if(newData.indexOf(ztreeId[i])==-1){newData.push(ztreeId[i])}
    				}
    			}else{
    				var del = treeNode.id;
    				var num =  newData.indexOf(del);
    				newData.splice(num,1);
    				
    			}
			};
			
			$.fn.zTree.init($("#role"), setting, zNodes);
			 
		})
		 

		 
		
		   
		  
	}
	
})